"""
@File    :   http_exception.py
@Time    :   2024/02/22 17:31:13
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   非自定义的响应状态异常处理
"""

from fastapi import Request
from fastapi.exceptions import HTTPException

from src.enums.http_response_code import HttpResponseEnum
from src.utils.unified_response import FailApiJSONResponse


async def http_exception_handler(request: Request, exc: HTTPException):
    """处理非自定义的响应状态异常
    以json形式返回
    """
    # 通过响应状态码获取对应的响应信息
    exc_msg = HttpResponseEnum.use_code_get_enum_msg(exc.status_code)
    # 构建响应信息
    response_dict: dict[str, int | str | dict] = {
        "status_code": exc.status_code,
        "code": exc.status_code,
        "message": exc_msg,
        # "data": exc.detail,
    }
    # 如果有改变headers, 则传入headers
    if exc.headers:
        response_dict["headers"] = exc.headers
    return FailApiJSONResponse(**response_dict)
